package com.cheng.example.atguigu.view2;


import java.util.concurrent.atomic.AtomicInteger;

/**
 * 007、volatile不保证原子性问题解决
 */
class Client7_MyData {
    AtomicInteger atomicInteger = new AtomicInteger();
    public void addAtomic() {
        atomicInteger.getAndIncrement();
    }
}

public class Client07 {

    public static void main(String[] args) throws InterruptedException {
        Client7_MyData client7MyData = new Client7_MyData();
        for (int i = 1; i <= 20; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    client7MyData.addAtomic();
                }
            }).start();
        }
        while (Thread.activeCount() > 2) {
//            Thread.sleep(100);
//            continue;
            Thread.yield();
        }
        System.out.println(client7MyData.atomicInteger.get());
    }

}
